using System;
using System.Web;
using System.Web.Mvc;

namespace FacialClub.Helpers
{
    /// <summary>
    /// URL Helper Extensions
    /// </summary>
    public static class UrlHelperExtensions
    {
        internal static Uri ActionFull(this UrlHelper urlHelper, string actionName)
        {
            return new Uri(HttpContext.Current.Request.Url, urlHelper.Action(actionName));
        }

        internal static Uri ActionFull(this UrlHelper urlHelper, string actionName, string controllerName)
        {
            return new Uri(HttpContext.Current.Request.Url, urlHelper.Action(actionName, controllerName));
        }


        /// <summary>
        /// AbsolutePath
        /// </summary>
        /// <param name="urlHelper"></param>
        /// <param name="relativeUrl"></param>
        /// <returns></returns>
        public static string AbsolutePath(this UrlHelper urlHelper, string relativeUrl)
        {
            var url = urlHelper.RequestContext.HttpContext.Request.Url;
            var uriBuilder = new UriBuilder(url.Scheme, url.Host, url.Port) { Path = relativeUrl };
            //string appPath = urlHelper.RequestContext.HttpContext.Request.ApplicationPath;

            //if (!uriBuilder.Path.StartsWith(appPath))
            //    uriBuilder.Path = appPath + uriBuilder.Path;

            return uriBuilder.Uri.ToString();
        }

        /// <summary>
        /// AppPath
        /// </summary>
        /// <param name="urlHelper"></param>
        /// <param name="relativePath"></param>
        /// <returns></returns>
        public static string AppPath(this UrlHelper urlHelper, string relativePath)
        {
            if (relativePath == null) return null;

            return VirtualPathUtility.ToAbsolute(relativePath, urlHelper.RequestContext.HttpContext.Request.ApplicationPath);
        }

        /// <summary>
        /// Returns an absolute url for an action
        /// </summary>
        /// <param name="url">UrlHelper</param>
        /// <param name="action"></param>
        /// <param name="routeValues"></param>
        /// <returns></returns>
        public static string AbsoluteAction(this UrlHelper url, string action, object routeValues)
        {
            var requestUrl = url.RequestContext.HttpContext.Request.Url;

            var absoluteAction = string.Format("{0}://{1}{2}",
                                                  requestUrl.Scheme,
                                                  requestUrl.Authority,
                                                  url.Action(action, routeValues));

            return absoluteAction;
        }

        /// <summary>
        /// AbsoluteAction
        /// </summary>
        /// <param name="url"></param>
        /// <param name="scheme"></param>
        /// <param name="action"></param>
        /// <param name="routeValues"></param>
        /// <returns></returns>
        public static string AbsoluteAction(this UrlHelper url, string scheme, string action, object routeValues)
        {
            var requestUrl = url.RequestContext.HttpContext.Request.Url;

            var absoluteAction = string.Format("{0}://{1}{2}",
                                                  scheme,
                                                  requestUrl.Authority,
                                                  url.Action(action, routeValues));

            return absoluteAction;
        }


        /// <summary>
        /// StyleSheet URL
        /// </summary>
        /// <param name="url"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static string StyleSheet(UrlHelper url, string filename)
        {
            return url.Content("~/Assets/StyleSheet/" + filename);
        }

        /// <summary>
        /// JavaScript URL
        /// </summary>
        /// <param name="url"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static string JavaScript(UrlHelper url, string filename)
        {
            return url.Content("~/Scripts/" + filename);
        }

        /// <summary>
        /// Get Site Root
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string SiteRoot(HttpContextBase context)
        {
            return SiteRoot(context, true);
        }

        /// <summary>
        /// Get Site Root
        /// </summary>
        /// <param name="context"></param>
        /// <param name="usePort"></param>
        /// <returns></returns>
        public static string SiteRoot(HttpContextBase context, bool usePort)
        {
            var port = context.Request.ServerVariables["SERVER_PORT"];
            if (usePort)
            {
                if (port == null || port == "80" || port == "443")
                    port = "";
                else
                    port = ":" + port;
            }
            var protocol = context.Request.ServerVariables["SERVER_PORT_SECURE"];
            if (protocol == null || protocol == "0")
                protocol = "http://";
            else
                protocol = "https://";

            var appPath = context.Request.ApplicationPath;
            if (appPath == "/")
                appPath = "";

            var sOut = protocol + context.Request.ServerVariables["SERVER_NAME"] + port + appPath;
            return sOut;

        }

        /// <summary>
        /// Return SiteRoot
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string SiteRoot(this UrlHelper url)
        {
            return SiteRoot(url.RequestContext.HttpContext);
        }


        /// <summary>
        /// Return SiteRoot
        /// </summary>
        /// <param name="pg"></param>
        /// <returns></returns>
        public static string SiteRoot(this ViewPage pg)
        {
            return SiteRoot(pg.ViewContext.HttpContext);
        }

        /// <summary>
        /// Return SiteRoot
        /// </summary>
        /// <param name="pg"></param>
        /// <returns></returns>
        public static string SiteRoot(this ViewUserControl pg)
        {
            var vpage = pg.Page as ViewPage;
            return vpage != null ? SiteRoot(vpage.ViewContext.HttpContext) : null;
        }

        /// <summary>
        /// Return SiteRoot
        /// </summary>
        /// <param name="pg"></param>
        /// <returns></returns>
        public static string SiteRoot(this ViewMasterPage pg)
        {
            return SiteRoot(pg.ViewContext.HttpContext);
        }

        /// <summary>
        /// Return URL
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetReturnUrl(HttpContextBase context)
        {
            var returnUrl = "";

            if (context.Request.QueryString["ReturnUrl"] != null)
            {
                returnUrl = context.Request.QueryString["ReturnUrl"];
            }
            return returnUrl;
        }

        /// <summary>
        /// Return URL
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static string GetReturnUrl(this UrlHelper helper)
        {
            return GetReturnUrl(helper.RequestContext.HttpContext);
        }

        /// <summary>
        /// Return URL
        /// </summary>
        /// <param name="pg"></param>
        /// <returns></returns>
        public static string GetReturnUrl(this ViewPage pg)
        {
            return GetReturnUrl(pg.ViewContext.HttpContext);
        }

        /// <summary>
        /// Return URL
        /// </summary>
        /// <param name="pg"></param>
        /// <returns></returns>
        public static string GetReturnUrl(this ViewMasterPage pg)
        {
            return GetReturnUrl(pg.Page as ViewPage);
        }

        /// <summary>
        /// Return URL
        /// </summary>
        /// <param name="pg"></param>
        /// <returns></returns>
        public static string GetReturnUrl(this ViewUserControl pg)
        {
            return GetReturnUrl(pg.Page as ViewPage);
        }
    }
}